package com.zhb.designmode.creationmode.prototype.deepcoding;

public class Person implements Cloneable{
    private String name;
    private Integer age;
    private Phone phone;

    public Person(String name, Integer age, Phone phone) {
        this.name = name;
        this.age = age;
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Phone getPhone() {
        return phone;
    }

    public void setPhone(Phone phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", phone=" + phone +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person p = (Person) super.clone();//如果不实现clonable接口，调用clone()方法会抛出CloneNotSupportException
        //引用类型的成员变量也进行一次拷贝
        Phone clone = (Phone)this.phone.clone();
        p.phone = clone;
        return p;
    }
}
